package com.maori.languageapp.fragments;

import java.io.File;

import com.maori.languageapp.LanguageAppSearchArea;
import com.maori.languageapp.Maori_MainActivity;
import com.maori.languageapp.R;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SearchView;

public class Maori_Main_Fragment extends Fragment {

	private static final String TAG = null;

	private static final Context Context = null;

	int tabIndex;

	private FragmentTabHost mTabHost;

	public Maori_Main_Fragment() {
		// TODO Auto-generated constructor stub
	}
	
	public Maori_Main_Fragment(int linkingIndex) {
		tabIndex = linkingIndex;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mTabHost = new FragmentTabHost(getActivity());

		// View rootView =
		// inflater.inflate(R.layout.maori_fragment_main,container, false);

		// mTabHost =
		// (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);

		mTabHost.setup(getActivity(), getChildFragmentManager(),
				R.layout.maori_fragment_main);

		Bundle arg1 = new Bundle();
		arg1.putInt("Discover", 1);
		mTabHost.addTab(mTabHost.newTabSpec("Discover")
				.setIndicator("Discover"), Maori_Fragment_Discover.class, arg1);

		Bundle arg2 = new Bundle();
		arg2.putInt("Capture", 2);
		mTabHost.addTab(mTabHost.newTabSpec("Capture").setIndicator("Capture"),
				Maori_Fragment_Capture.class, arg2);

		Bundle arg3 = new Bundle();
		arg3.putInt("Curate", 3);
		mTabHost.addTab(mTabHost.newTabSpec("Curate").setIndicator("Curate"),
				Maori_Fragment_Curate.class, arg3);

		mTabHost.setCurrentTab(tabIndex);
		if (tabIndex == 8) {
			Log.v(TAG, "*********CALL SYNC**********");
			syncServerData();
		} else {
			Log.v(TAG, "*********CALL TABS**********");
			mTabHost.setCurrentTab(tabIndex);
		}
		return mTabHost;
	}

	private void syncServerData() {
		// TODO Auto-generated method stub
		/*
		 * Intent intentDirectLog = new Intent(Context,
		 * Maori_MainActivity.class);
		 * intentDirectLog.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		 * intentDirectLog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 * startActivity(intentDirectLog);
		 */
		Intent intent = new Intent(getActivity(), Maori_MainActivity.class);
		intent.putExtra("SYNC", "ACTIVE");
		startActivity(intent);
	}

	// ***********Memory Handling****************
	@Override
	public void onStop() {
		super.onStop();
	}

	// Fires after the OnStop() state
	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			trimCache(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void trimCache(Maori_Main_Fragment maori_Main_Fragment) {
		try {
			File dir = maori_Main_Fragment.getCacheDir();
			if (dir != null && dir.isDirectory()) {
				deleteDir(dir);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private File getCacheDir() {
		// TODO Auto-generated method stub
		return null;
	}

	public static boolean deleteDir(File dir) {
		if (dir != null && dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}

	void OnBackStackChangedListener() {

	}
}
